package com.fowo.api.exportCenter.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.exportCenter.model.ExportVo;
import com.fowo.api.exportCenter.service.ExportCommonService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "导出中心")
@RequestMapping("/api/common")
@RestController
public class ExportCommonController {
    @Autowired
    private ExportCommonService exportCommonService;
    @Operation(summary = "通用导出 异步")
    @PostMapping("export")
    public R export(
            @RequestBody ExportVo exportVo
    ) throws Exception {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (exportVo.getExportApi() != null) {
            exportVo.setUrl("/api/"+exportVo.getUrl() + "/" + exportVo.getExportApi());
        } else {
            exportVo.setUrl("/api/"+exportVo.getUrl()+"/export");
        }
        String token=JwtUserInfo.getToken();
        Thread t=new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                exportCommonService.export3(exportVo,jwtUserInfo.getUserId(),token);
            }
        });
        t.start();
        return R.success("使用异步导出成功，请稍后查看",null);
    }

}
